package com.yuanjin.activity;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.yuanjin.R;
import com.yuanjin.main.MainActivity;
import com.yuanjin.util.LoaddataThread;
import com.yuanjin.util.YuanJin_Application;
import com.yuanjin.util.ToastHelper;
import com.yuanjin.util.Yuanjin;

public class LoginPage extends Activity {
	// 请求服务端的url
	private static String PATH = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=login";
	// http://1.zhan414.sinaapp.com/?/index.php?c=main&a=write
	private static URL url;
	private TextView TVusername;
	private TextView TVpassword;
	private Button mButton;
	private String data = "";
	private int sum = 0;

	private static final int requestCode = 1;

	static {
		try {
			url = new URL(PATH);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				Toast.makeText(LoginPage.this, "注册成功", Toast.LENGTH_SHORT)
						.show();
				break;
			case 2:
				Toast.makeText(LoginPage.this, "该用户名已被注册", Toast.LENGTH_SHORT)
						.show();
				break;
			case 3:
				Toast.makeText(LoginPage.this, "用户名不存在，请重试或注册新账户",
						Toast.LENGTH_LONG).show();
				break;
			case 4:
				Toast.makeText(LoginPage.this, "密码不正确，请重新输入",
						Toast.LENGTH_SHORT).show();
				break;
			case 5:
				// Toast.makeText(LoginPage.this, "登录验证通过", Toast.LENGTH_SHORT)
				// .show();
				ToastHelper.makeTextShow(LoginPage.this, "登录验证通过",
						ToastHelper.LENGTH_SHORT);
				/*
				 * receiveData:服务器传过来的用户数据 receiveData_Sum：用户数据的数目
				 */
				YuanJin_Application md = (YuanJin_Application) getApplication();
				data = (String) msg.obj;
				sum = msg.arg1;
				md.setReceiveData(data);
				md.setReceiveData_Sum(sum);

				Intent intent2 = new Intent(LoginPage.this, MainActivity.class);
				startActivity(intent2);
				break;
			// case 6:
			//
			// /*
			// * receiveData:服务器传过来的用户数据 receiveData_Sum：用户数据的数目
			// */
			// Toast.makeText(LoginPage.this,
			// "cccccccccccccccccccccccchhhhhhhhhh", Toast.LENGTH_SHORT)
			// .show();
			// sum = msg.arg1;
			// md.setReceiveData_Sum(sum);
			// Toast.makeText(LoginPage.this, "cccccccccccccccccccccccc",
			// Toast.LENGTH_SHORT)
			// .show();
			// break;
			case 8:

				/*
				 * receiveData:服务器传过来的用户数据 receiveData_Sum：用户数据的数目
				 */

				sum = msg.arg1;
				YuanJin_Application.message_sum = Integer.toString(sum);
				Log.e("logtest_firstin",
						"/******************* case 8 **********************/"
								+ YuanJin_Application.message_sum);

				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login2);

		/* 设置mButton的onClick事件处理 */
		mButton = (Button) findViewById(R.id.loginbutton);
		TVpassword = (TextView) findViewById(R.id.login_password);
		TVusername = (TextView) findViewById(R.id.login_username);

	}

	public void gotoRegisterPage(View view) {
		Intent intent = new Intent(this, RegisterPage.class);
		startActivityForResult(intent, requestCode);
	}

	public void LoginClick(View view) {

		SharedPreferences sp = getSharedPreferences(
				Yuanjin.YuanjinSharedPreference, Activity.MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString("username", TVusername.getText().toString());
		editor.putString("password", TVpassword.getText().toString());
		editor.commit();
		// 将用户名存到本地
		YuanJin_Application yd = new YuanJin_Application();
		yd.setUsername(TVusername.getText().toString());

		Yuanjin.username = TVusername.getText().toString();

		// 上传服务器的账号、密码等信息
		Map<String, String> map = new HashMap<String, String>();
		map.put("username", TVusername.getText().toString());
		map.put("password", TVpassword.getText().toString());
		// map.put("message_sum", yd.message_sum);
		// 上传服务器的调用方法地址
		String path = "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=login";
		if (TVusername.getText().toString() == "") {
			Toast.makeText(LoginPage.this, "请输入用户名", Toast.LENGTH_SHORT).show();
		} else {
			// 开启上传线程
			LoaddataThread loaddataThread = new LoaddataThread(map, handler,
					path);
			loaddataThread.start();
		}
		//
		/*********************
		 * 向服务器上传数据 -- 获取服务器数据库的记录数目
		 ***************************/
		// // " datesecond INTEGER," + " date VARCHAR(20),"
		// // + " content TEXT)";
		// // 上传服务器的账号、密码等信息
		// map = new HashMap<String, String>();
		// // map.put("datesecond", currentTimeSecond);
		// map.put("message_sum", "0");
		// map.put("content", "r#");
		// map.put("notice", "您已成功添加了该好友");
		// map.put("date",
		// DateFormat.format("yyyy.MM.dd hh.mm",
		// System.currentTimeMillis()).toString());
		// map.put("username", "system");
		// // 上传服务器的调用方法地址
		// path =
		// "http://1.zhan414.sinaapp.com/?/index.php?c=main&a=store_chatmessage";
		// Log.e("message", "到这里都没错");
		// // 开启上传线程
		// LoaddataThread loaddataThread = new LoaddataThread(map, handler,
		// path);
		// loaddataThread.start();

	}
}
